package cn.edu.xhu.library_management.area.controller;

import cn.edu.xhu.library_management.area.entity.LmInfo;
import cn.edu.xhu.library_management.area.service.InfoService;
import cn.edu.xhu.library_management.common.entity.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2022 - 10 - 01
 * @Description: 图书馆信息Controller
 */
@Api(tags = "图书馆信息模块")
@ApiSort(1)
@RestController
@RequestMapping("/lm_area")
public class InfoController {

    @Autowired
    private InfoService infoService;

    @GetMapping("/info")
    @ApiOperation(value = "查询所有图书馆信息",produces="application/json")
    @ApiOperationSupport(order = 111)
    public R findAllInfos(){
        List<LmInfo> infos = infoService.findAllInfos();
        return !infos.isEmpty()?R.ok().data("infos",infos): R.error("数据为空");
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "通过id查询图书馆信息",produces="application/json")
    @ApiOperationSupport(order = 112)
    public R findInfoById(@PathVariable("id") Integer id){
        LmInfo info = infoService.findInfoById(id);
        return info !=null ? R.ok().data("info",info):R.error("查无此数据!!!");
    }



    @PostMapping("/info")
    @ApiOperation(value = "添加图书馆信息",produces="application/json")
    @ApiOperationSupport(order = 121)
    public R addInfo(@RequestBody LmInfo info){
        return infoService.addInfo(info)? R.ok("添加图书馆信息成功!!!"):R.error("添加图书馆信息失败!!!");
    }

    @PutMapping("/info")
    @ApiOperation(value = "修改图书馆信息",produces="application/json")
    @ApiOperationSupport(order = 131)
    public R updateInfo(@RequestBody LmInfo info){
        return infoService.updateInfo(info)?R.ok("修改图书馆信息成功!!!"):R.error("修改图书馆信息失败!!!");
    }

    @DeleteMapping("/info/{id}")
    @ApiOperation(value = "删除图书馆信息",produces="application/json")
    @ApiOperationSupport(order = 141)
    public R deleteInfoById(@PathVariable("id") Integer id){
        return infoService.deleteInfoById(id)?R.ok("删除图书馆信息成功!!!"):R.error("删除图书馆信息失败!!!");
    }
}
